﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.BLL;
using XiaoGang.Soft.XGWMS.MODEL;
using XiaoGang.Soft.XGWMS.COM;
using System.IO;
using System.Drawing.Imaging;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M003UserEdit : FormBase
    {
        #region 定义变量

        bool blFormLoad = false;
        enumOperation MOpertion = new enumOperation();

        /// <summary>
        /// 用户
        /// </summary>
        private const string dataName = "用户";
        private readonly MOD_M003_USER modUser = null;
        private readonly BLL_M003_USER bllUser = null;

        #endregion

        #region 方法

        public M003UserEdit()
        {
            InitializeComponent();
            bllUser = new BLL_M003_USER();
            modUser = new MOD_M003_USER();
        }

        public M003UserEdit(MOD_M003_USER userModel)
        {
            InitializeComponent();
            modUser = userModel;
            bllUser = new BLL_M003_USER();
        }

        /// <summary>
        /// Initial
        /// </summary>
        private void Initial()
        {
            SetControlsIndex();
            InitialAllCombox();
            InitialControl();            
            txtUSER_ID.Enabled = false;
        }

        /// <summary>
        /// SetControlsIndex
        /// </summary>
        protected void SetControlsIndex()
        {
            int intIndex = 0; intIndex++;
            txtUSER_ID.TabIndex = intIndex; intIndex++;
            txtUSER_LOGIN_ID.TabIndex = intIndex; intIndex++;
            txtUSER_WD.TabIndex = intIndex; intIndex++;
            txtUSER_NAME.TabIndex = intIndex; intIndex++;
            cmbUSER_GROUP.TabIndex = intIndex; intIndex++;
            txtUSER_BIRTHDAY.TabIndex = intIndex; intIndex++;
            txtUSER_PHONE1.TabIndex = intIndex; intIndex++;
            txtUSER_PHONE2.TabIndex = intIndex; intIndex++;
            grpSex.TabIndex = intIndex; intIndex++;
            txtUSER_ADDRESS1.TabIndex = intIndex; intIndex++;
            txtUSER_ADDRESS2.TabIndex = intIndex; intIndex++;
            txtUSER_EMAIL.TabIndex = intIndex; intIndex++;
            txtUSER_POST.TabIndex = intIndex; intIndex++;
            grpState.TabIndex = intIndex; intIndex++;
            txtMEMO.TabIndex = intIndex; intIndex++;
            btnPicSearch.TabIndex = intIndex; intIndex++;
            btnPicClear.TabIndex = intIndex; intIndex++;
            btnAdd.TabIndex = intIndex; intIndex++;
            btnClear.TabIndex = intIndex; intIndex++;
            btnExit.TabIndex = intIndex;
        }

        /// <summary>
        /// InitialControl
        /// </summary>
        private void InitialControl()
        {
            lbImageMaxSize.Text = lbImageMaxSize.Text.Replace("@", GlobalVal.gloImageMaxSize.ToString());
            this.txtUSER_ID.Text = modUser.USER_ID;
            this.txtUSER_LOGIN_ID.Text = modUser.USER_LOGIN_ID;
            this.txtUSER_WD.Text = modUser.USER_WD;
            this.txtUSER_NAME.Text = modUser.USER_NAME;
            this.cmbUSER_GROUP.SelectedValue = modUser.USER_GROUP;
            this.dateTimePickerUSER_BIRTHDAY.Text = modUser.USER_BIRTHDAY;
            this.txtUSER_BIRTHDAY.Text = modUser.USER_BIRTHDAY;
            this.txtUSER_PHONE1.Text = modUser.USER_PHONE1;
            this.txtUSER_PHONE2.Text = modUser.USER_PHONE2;
            if (string.IsNullOrEmpty(modUser.USER_SEX) || modUser.USER_SEX == "0")		
	        {		
	            rdoBSEX_M.Checked = true;		
	        }		
	        else		
	        {		
	            rdoBSEX_F.Checked = true;		
	        }
            this.txtUSER_ADDRESS1.Text = modUser.USER_ADDRESS1;
            this.txtUSER_ADDRESS2.Text = modUser.USER_ADDRESS2;
            this.txtUSER_EMAIL.Text = modUser.USER_EMAIL;
            this.txtUSER_POST.Text = modUser.USER_POST;
            if (string.IsNullOrEmpty(modUser.USER_STATE) || modUser.USER_STATE == "0")		
	        {		
	            rdbUSER_STATE1.Checked = true;		
	        }		
	        else		
	        {		
	            rdbUSER_STATE2.Checked = true;		
	        }
            this.txtMEMO.Text = modUser.MEMO;
            ShowImage(modUser.USER_ID);
        }

        /// <summary>
        /// InitialAllCombox
        /// </summary>
        private void InitialAllCombox()
        {
            Method.CmbDataBound("User_Group_ID", cmbUSER_GROUP);
            this.cmbUSER_GROUP.SelectedIndex = 0;
        }

        /// <summary>
        /// ShowImage
        /// </summary>
        /// <param name="UserID"></param>
        private void ShowImage(string UserID)
        {
            if (string.IsNullOrEmpty(UserID))
            {
                this.picUSER_PHOTO.Image = null;
                return;
            }
            try
            {
                ReturnValue returnValue = bllUser.GetUserPhoto(UserID);
                if (!returnValue.IsSuccess)
                {
                    return;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                MemoryStream ms = new MemoryStream((byte[])returnValue.ResultDataSet.Tables[0].Rows[0]["USER_PHOTO"]);
                Image image = Image.FromStream(ms, true);
                this.picUSER_PHOTO.Image = image;
            }
            catch
            {
                this.picUSER_PHOTO.Image = null;
            }
        }

        /// <summary>
        /// CheckProcessLoop
        /// </summary>
        /// <param name="enumOperation"></param>
        /// <returns></returns>
        protected bool CheckProcessLoop(enumOperation enumOperation)
        {
            switch (enumOperation)
            {                
                case enumOperation.Add:
                    MOpertion = enumOperation.Add;
                    //登录ID
                    if (!CheckProcess(this.txtUSER_LOGIN_ID.Name)) return false;
                    //密码
                    if (!CheckProcess(this.txtUSER_WD.Name)) return false;
                    //姓名
                    if (!CheckProcess(this.txtUSER_NAME.Name)) return false;
                    //组别
                    if (!CheckProcess(this.cmbUSER_GROUP.Name)) return false;
                    //出生年月日
                    if (!CheckProcess(this.txtUSER_BIRTHDAY.Name)) return false;
                    //电话1
                    if (!CheckProcess(this.txtUSER_PHONE1.Name)) return false;
                    //电话2
                    if (!CheckProcess(this.txtUSER_PHONE2.Name)) return false;
                    //住址1
                    if (!CheckProcess(this.txtUSER_ADDRESS1.Name)) return false;
                    //住址2
                    if (!CheckProcess(this.txtUSER_ADDRESS2.Name)) return false;
                    //E_mail
                    if (!CheckProcess(this.txtUSER_EMAIL.Name)) return false;
                    //邮编
                    if (!CheckProcess(this.txtUSER_POST.Name)) return false;
                    //备注
                    if (!CheckProcess(this.txtMEMO.Name)) return false;
                    break;
                case enumOperation.Update:
                    MOpertion = enumOperation.Update;
                    //编号
                    if (!CheckProcess(this.txtUSER_ID.Name)) return false;
                    //登录ID
                    if (!CheckProcess(this.txtUSER_LOGIN_ID.Name)) return false;
                    //密码
                    if (!CheckProcess(this.txtUSER_WD.Name)) return false;
                    //姓名
                    if (!CheckProcess(this.txtUSER_NAME.Name)) return false;
                    //组别
                    if (!CheckProcess(this.cmbUSER_GROUP.Name)) return false;
                    //出生年月日
                    if (!CheckProcess(this.txtUSER_BIRTHDAY.Name)) return false;
                    //电话1
                    if (!CheckProcess(this.txtUSER_PHONE1.Name)) return false;
                    //电话2
                    if (!CheckProcess(this.txtUSER_PHONE2.Name)) return false;
                    //住址1
                    if (!CheckProcess(this.txtUSER_ADDRESS1.Name)) return false;
                    //住址2
                    if (!CheckProcess(this.txtUSER_ADDRESS2.Name)) return false;
                    //E_mail
                    if (!CheckProcess(this.txtUSER_EMAIL.Name)) return false;
                    //邮编
                    if (!CheckProcess(this.txtUSER_POST.Name)) return false;
                    //备注
                    if (!CheckProcess(this.txtMEMO.Name)) return false;
                    break;
            }
            return true;
        }

        /// <summary>
        /// CheckProcess
        /// </summary>
        /// <param name="strConstrolName"></param>
        /// <returns></returns>
        protected override bool CheckProcess(string strConstrolName)
        {
            string strValue = string.Empty;
            switch (strConstrolName)
            {
                #region 编号
                case "txtUSER_ID":
                    strValue = txtUSER_ID.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemID, true, null))
                        {
                            txtUSER_ID.Focus();
                            txtUSER_ID.SelectAll();
                            return false;
                        }
                    }
                    else if (MOpertion == enumOperation.Add)
                    {
                        break;
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemID, false, null))
                        {
                            txtUSER_ID.Focus();
                            txtUSER_ID.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 登录ID
                case "txtUSER_LOGIN_ID":
                    strValue = txtUSER_LOGIN_ID.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.USER_LOGIN_ID, true, null))
                        {
                            txtUSER_LOGIN_ID.Focus();
                            txtUSER_LOGIN_ID.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.USER_LOGIN_ID, false, null))
                        {
                            txtUSER_LOGIN_ID.Focus();
                            txtUSER_LOGIN_ID.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 密码
                case "txtUSER_WD":
                    strValue = txtUSER_WD.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.PassWord, true, null))
                        {
                            txtUSER_WD.Focus();
                            txtUSER_WD.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.PassWord, false, null))
                        {
                            txtUSER_WD.Focus();
                            txtUSER_WD.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 姓名
                case "txtUSER_NAME":
                    strValue = txtUSER_NAME.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.UserName, true, null))
                        {
                            txtUSER_NAME.Focus();
                            txtUSER_NAME.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.UserName, false, null))
                        {
                            txtUSER_NAME.Focus();
                            txtUSER_NAME.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 组别
                case "cmbUSER_GROUP":
                    strValue = cmbUSER_GROUP.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.UserGroup, true, null))
                        {
                            cmbUSER_GROUP.Focus();
                            cmbUSER_GROUP.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.UserGroup, false, null))
                        {
                            cmbUSER_GROUP.Focus();
                            cmbUSER_GROUP.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 出生年月日
                case "txtUSER_BIRTHDAY":
                    strValue = txtUSER_BIRTHDAY.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.DateYMD, true, null))
                    {
                        txtUSER_BIRTHDAY.Focus();
                        txtUSER_BIRTHDAY.SelectAll();
                        return false;
                    }
                    break;

                #endregion

                #region 电话1
                case "txtUSER_PHONE1":
                    strValue = txtUSER_PHONE1.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.PHONE_NO, true, null))
                        {
                            txtUSER_PHONE1.Focus();
                            txtUSER_PHONE1.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.PHONE_NO, false, null))
                        {
                            txtUSER_PHONE1.Focus();
                            txtUSER_PHONE1.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 电话2
                case "txtUSER_PHONE2":
                    strValue = txtUSER_PHONE2.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.PHONE_NO, true, null))
                    {
                        txtUSER_PHONE2.Focus();
                        txtUSER_PHONE2.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 住址1
                case "txtUSER_ADDRESS1":
                    strValue = txtUSER_ADDRESS1.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.ItemMEMO, true, null))
                    {
                        this.txtUSER_ADDRESS1.Focus();
                        txtUSER_ADDRESS1.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 住址2
                case "txtUSER_ADDRESS2":
                    strValue = txtUSER_ADDRESS2.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.ItemMEMO, true, null))
                    {
                        this.txtUSER_ADDRESS2.Focus();
                        txtUSER_ADDRESS2.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region E_mail
                case "txtUSER_EMAIL":
                    strValue = txtUSER_EMAIL.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.E_Mail, true, null))
                    {
                        this.txtUSER_EMAIL.Focus();
                        txtUSER_EMAIL.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 邮编
                case "txtUSER_POST":
                    strValue = txtUSER_POST.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.Post, true, null))
                    {
                        this.txtUSER_POST.Focus();
                        this.txtUSER_POST.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 备注
                case "txtMEMO":
                    strValue = txtMEMO.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.ItemMEMO, true, null))
                    {
                        this.txtMEMO.Focus();
                        txtMEMO.SelectAll();
                        return false;
                    }
                    break;
                #endregion
            }
            return true;
        }

        #endregion

        #region 事件

        /// <summary>
        /// M003_USER_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M003_USER_Load(object sender, EventArgs e)
        {
            blFormLoad = true;
            Initial();
        }

        /// <summary>
        /// btnClear_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            InitialControl();
        }

        /// <summary>
        /// btnExit_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Close();
        }        

        /// <summary>
        /// M003_USER_KeyUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M003_USER_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    btnAdd_Click(sender, e);
                    break;
                case Keys.F12:
                    btnClear_Click(sender, e);
                    break;
                case Keys.Escape:
                    btnExit_Click(sender, e);
                    break;
            }
        }

        /// <summary>
        /// btnAdd_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (modUser.USER_ID.Length < 1 && !this.CheckProcessLoop(enumOperation.Add))
            {
                return;
            }
            if (modUser.USER_ID.Length > 0 && !this.CheckProcessLoop(enumOperation.Update))
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            ReturnValue returnValue;
            modUser.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID;
            modUser.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD;
            modUser.USER_ID = this.txtUSER_ID.Text.Trim();
            modUser.USER_LOGIN_ID = this.txtUSER_LOGIN_ID.Text.Trim();
            modUser.USER_WD = this.txtUSER_WD.Text.Trim();
            modUser.USER_NAME = this.txtUSER_NAME.Text.Trim();
            if (rdoBSEX_M.Checked)
            {
                modUser.USER_SEX = "0";
            }
            else
            {
                modUser.USER_SEX = "1";
            }
            modUser.USER_BIRTHDAY = this.txtUSER_BIRTHDAY.Text.Trim().Replace("/", "");
            modUser.USER_PHONE1= this.txtUSER_PHONE1.Text.Trim();
            modUser.USER_PHONE2 = this.txtUSER_PHONE2.Text.Trim();
            modUser.USER_ADDRESS1 = this.txtUSER_ADDRESS1.Text.Trim();
            modUser.USER_ADDRESS2 = this.txtUSER_ADDRESS2.Text.Trim();
            modUser.USER_EMAIL = this.txtUSER_EMAIL.Text.Trim();
            modUser.USER_POST = this.txtUSER_POST.Text.Trim();
            //************************************************
            if (picUSER_PHOTO.Image != null)
            {
                MemoryStream ms = new MemoryStream();
                picUSER_PHOTO.Image.Save(ms, ImageFormat.Jpeg);
                Byte[] buffer = new Byte[ms.Length];
                ms.Position = 0;
                ms.Read(buffer, 0, Convert.ToInt32(ms.Length));
                modUser.USER_PHOTO = buffer;
            }
            //************************************************
            else
            {
                modUser.USER_PHOTO = null;
            }
            modUser.USER_GROUP = this.cmbUSER_GROUP.SelectedValue.ToString();
            if(rdbUSER_STATE1.Checked)
            {
                modUser.USER_STATE = "0";
            }
            else
            {
                modUser.USER_STATE = "1";
            }
            modUser.MEMO =this.txtMEMO.Text.Trim();
            try
            {
                returnValue = bllUser.AddOrUpdateUser(modUser);
                if (!returnValue.IsSuccess)
                {
                    Method.MessageShow(returnValue.ErrorID, dataName);
                    this.Cursor = Cursors.Default;
                    return;
                }
                else
                {
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
            catch(Exception ex)
            {
                Method.MessageShow("E11004",dataName);//发生未知错误！
            }
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// btnPicSearch_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPicSearch_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "所有图片(*.bmp;*.gif;*.jpeg;*.jpg;*.png)|*.bmp;*.gif;*.jpeg;*.jpg;*.png";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
                if (fileInfo.Length > 1024*GlobalVal.gloImageMaxSize)
                {
                    Method.MessageShow("W11020",dataName);//Image size must be less than 100 KB.
                    return;
                }
                else
                {
                    this.picUSER_PHOTO.Image = Image.FromFile(openFileDialog.FileName);
                }
            }
        }

        /// <summary>
        /// btnPicClear_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPicClear_Click(object sender, EventArgs e)
        {
            this.picUSER_PHOTO.Image = null;
        }
       
        /// <summary>
        /// dateTimePickerUSER_BIRTHDAY_CloseUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dateTimePickerUSER_BIRTHDAY_CloseUp(object sender, EventArgs e)
        {
            txtUSER_BIRTHDAY.Text = Method.TimeFormat(dateTimePickerUSER_BIRTHDAY.Value.ToString("yyyyMMdd"));
        }

        /// <summary>
        /// 重获焦点后刷新用户组列表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M003_USER_Activated(object sender, EventArgs e)
        {
            if (!blFormLoad)
            {
                string strValue = cmbUSER_GROUP.SelectedValue.ToString();
                Method.CmbDataBound("User_Group_ID", cmbUSER_GROUP);
                foreach (System.Data.DataRowView dr in this.cmbUSER_GROUP.Items)
                {
                    if (strValue == dr["GROUP_ID"].ToString().Trim())
                    {
                        cmbUSER_GROUP.SelectedValue = strValue;
                        return;
                    }
                }
                cmbUSER_GROUP.SelectedIndex = 0;
            }
            blFormLoad = false;
        }

        /// <summary>
        /// 打开用户组编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUserGroupEdit_Click(object sender, EventArgs e)
        {
            if(new M002UserGroupEdit().ShowDialog()== System.Windows.Forms.DialogResult.OK)
            {
                M003_USER_Activated(null,null);
            }
        }

        #endregion
    }
}